package com.zyd.shiro.business.service;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.zyd.shiro.persistence.beans.TrainUser;
import com.zyd.shiro.persistence.dao.MallTrainUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.zyd.shiro.persistence.dao.MallTrainMeetingDao;
import com.zyd.shiro.persistence.beans.TrainMeeting;

/**
 * 服务层
 * 
 * @author RookieWZW
 *
 */
@Service
public class MallTrainMeetingService {

	@Autowired
	private MallTrainMeetingDao trainMeetingDao;



	public List<TrainMeeting> findAll() {
		return trainMeetingDao.findAll();
	}


	/**
	 * 按时间顺序查找会议，过期不显示
	 * @param nowDate
	 * @return
	 */
	public List<TrainMeeting> findAllNeedShowASC(Date nowDate, int type)
	{

		return trainMeetingDao.findAllByMeeting_dateAfterASC(nowDate,type);

	}
	public List<TrainMeeting> findAllDESC( int type)
	{
		return trainMeetingDao.findAllDESC(type);
	}
	/**
	 * 按时间逆序查找会议，过期不显示
	 * @param nowDate
	 * @return
	 */
	public List<TrainMeeting> findAllNeedShowDESC(Date nowDate,int type)
	{
		return trainMeetingDao.findAllByMeeting_dateAfterDESC(nowDate,type);

	}



	/**
	 *  private int id;//
     java.util.Date meeting_date;//日期
	 String meeting_title;//题目
	 String meeting_theme;//主题
	 String meeting_place;//地点
	 String meeting_content;//内容
	 int meeting_type;//0会议  1培训
	 int train_price;//培训价格
	 java.util.Date create_time;//创建时间
	 *
	 */
	/**
	 * 分类查询
	 * @param
	 * @return
	 */
	public List<TrainMeeting> findAllByThemeASC(String theme,Date date,int type)
	{
		return trainMeetingDao.findAllByMeeting_themeEqualsASC(theme,date,type);
	}
	public List<TrainMeeting> findAllByThemeDESC(String theme,Date date,int type)
	{
		return trainMeetingDao.findAllByMeeting_themeEqualsDESC(theme,date,type);
	}
	public List<TrainMeeting> findAllByPlaceASC(String place,Date date,int type)
	{
		return  trainMeetingDao.findAllByMeeting_placeEqualsASC(place,date,type);
	}
	public List<TrainMeeting> findAllByPlaceDESC(String place,Date date,int type)
	{
		return  trainMeetingDao.findAllByMeeting_placeEqualsDESC(place,date,type);
	}
	public List<TrainMeeting> findAllByTitleASC(String title,int type)
	{
		return trainMeetingDao.findAllByMeeting_titleLikeASC(title,type);
	}
	public List<TrainMeeting> findAllByTitleDESC(String title,int type)
	{
		return trainMeetingDao.findAllByMeeting_titleLikeDESC(title,type);
	}
	public int IshaveTheme(String theme)
	{
		return trainMeetingDao.IshaveTheme(theme);
	}
	public void addTheme(String theme){
		trainMeetingDao.addTheme(theme);
	}
	public List<String> findAllTheme()
	{
		return trainMeetingDao.findAllTheme();
	}







	private Specification<TrainMeeting> where(Map searchMap) {

		return new Specification<TrainMeeting>() {

			@Override
			public Predicate toPredicate(Root<TrainMeeting> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();

				// 题目
				if (searchMap.get("meeting_title")!=null && !"".equals(searchMap.get("meeting_title"))) {
					predicateList.add(cb.like(root.get("meeting_title").as(String.class), "%"+(String)searchMap.get("meeting_title")+"%"));
				}
				// 内容
				if (searchMap.get("meeting_content")!=null && !"".equals(searchMap.get("meeting_content"))) {
					predicateList.add(cb.like(root.get("meeting_content").as(String.class), "%"+(String)searchMap.get("meeting_content")+"%"));
				}
				//
				if (searchMap.get("meeting_theme")!=null && !"".equals(searchMap.get("meeting_theme"))) {
					predicateList.add(cb.like(root.get("meeting_theme").as(String.class), "%"+(String)searchMap.get("meeting_theme")+"%"));
				}
				//
				if (searchMap.get("meeting_place")!=null && !"".equals(searchMap.get("meeting_place"))) {
					predicateList.add(cb.like(root.get("meeting_place").as(String.class), "%"+(String)searchMap.get("meeting_place")+"%"));
				}
				if (searchMap.get("meeting_type")!=null && !"".equals(searchMap.get("meeting_type"))) {
					predicateList.add(cb.equal(root.get("meeting_type").as(Integer.class), (Integer.valueOf((Integer) searchMap.get("meeting_type")))));
				}


				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}
	/**
	 * 分页查询
	 *
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<TrainMeeting> findPage(int page, int size,int type) {
		Map whereMap = new HashMap<>();
		whereMap.put("meeting_type",type);
		Specification<TrainMeeting> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return trainMeetingDao.findAll(specification, pageRequest);
	}

	public Page<TrainMeeting> findSearch(String whereString, int page, int size,int type) {
		Map whereMap = new HashMap<>();
		whereMap.put("meeting_title",whereString);
		whereMap.put("meeting_type",type);
		Specification<TrainMeeting> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return trainMeetingDao.findAll(specification, pageRequest);
	}

	public TrainMeeting findOne(Integer id) {
		return trainMeetingDao.findOne(id);
	}

	public void add(TrainMeeting trainMeeting) {

		trainMeetingDao.save(trainMeeting);
	}

	public void update(TrainMeeting trainMeeting) {
		trainMeetingDao.save(trainMeeting);
	}

	public void delete(Integer id) {
		trainMeetingDao.delete(id);
	}

	public void deleteList(Integer[] ids) {
		for (Integer id : ids) {
			trainMeetingDao.delete(id);
		}
	}

}
